package com.longge;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MaximumUnits {

    public int maximumUnits(int[][] boxTypes, int truckSize) {

        List<int[]> sortInts = Stream.of(boxTypes).sorted((arr1, arr2) -> arr2[1] - arr1[1]).collect(Collectors.toList());
        int ans = 0;
        for (int[] sortInt : sortInts) {
            if (truckSize != 0) {
                int count = sortInt[0];
                int weight = sortInt[1];
                if (truckSize - count < 0) {
                    ans += truckSize * weight;
                    return ans;
                } else {

                    ans += count * weight;
                    truckSize -= count;
                }

            }


        }
        return ans;
    }

    public static void main(String[] args) {
        //[1,3],[2,2],[3,1]
        int[][] param = {{1, 3}, {2, 2}, {3, 1}};
        new MaximumUnits().maximumUnits(param,4);

    }


}
